//
//  ArchiveInfoViewController.m
//  安好
//
//  Created by lianchuang on 14-8-26.
//  Copyright (c) 2014年 冯凯. All rights reserved.
//

#import "ArchiveInfoViewController.h"
#import "ArchiveViewCell.h"
#import "FamilyHistoryCell.h"
#import "InviteFriendsViewController.h"
#import "MyPickerView.h"
@interface ArchiveInfoViewController ()<MyPickerViewDelegate>

@end
typedef enum {
    selectSex = 1,
    selectAge,
    selectHeight,
    selectWeight
}SelectType;
@implementation ArchiveInfoViewController
{
    NSMutableArray *sexArray;
    NSMutableArray *heightArray;
    NSMutableArray *weightArray;
    NSMutableArray *ageArray;
    NSMutableDictionary *birthdayDic;
    NSMutableArray *pickerViewDate;
    NSInteger index;
    NSMutableArray *cellArray;
    UITextField *temptef;
    NSString *ID;
    MyPickerView *myPickerView;
    NSString *selectedString;//选中的字符串
    NSString *beforeSelectedString;//选中前的字符串
    SelectType selectedType;

}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
-(void)makeNavBar{
    self.navigationItem.title = @"设置";
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(backBtnclicked)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
}
- (void)backBtnclicked
{
//    [self myPickerViewCancelSelected:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)creatUI
{
    
    //性别数据源
    sexArray = [[NSMutableArray alloc]initWithObjects:@"男",@"女", nil];
    //年龄数据源
//    ageArray = [[NSMutableArray alloc]init];
//    for (NSInteger i=1; i<=130; i++) {
//        [ageArray addObject:[NSString stringWithFormat:@"%d岁",i]];
//    }
    //身高数据源
    heightArray = [[NSMutableArray alloc]init];
    for (NSInteger i=100; i<=300; i++) {
        [heightArray addObject:[NSString stringWithFormat:@"%ldcm",i]];
    }
    //体重数据源
    weightArray = [[NSMutableArray alloc]init];
    for (NSInteger i=30; i<=150; i++) {
        [weightArray addObject:[NSString stringWithFormat:@"%ldkg",i]];
    }
//    pickerViewDate=[[NSMutableArray alloc]init];
    cellArray=[[NSMutableArray alloc]init];
    
    
    //创建自定义pickerView
    myPickerView = [[MyPickerView alloc]init];
    myPickerView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 200);
    myPickerView.delegate = self;
    for (int i=0;i<3; i++) {
        FamilyHistoryCell *cell;
        if (IPhone6Plus) {
            cell=[[[NSBundle mainBundle]loadNibNamed:@"FamilyHistoryCell" owner:self options:nil] lastObject];
        }else{
            cell=[[[NSBundle mainBundle]loadNibNamed:@"FamilyHistoryCell" owner:self options:nil] firstObject];
        }
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        
        UITextField *textfeild=[[UITextField alloc]initWithFrame:CGRectMake(101, kScreenWidth==414?10: 7, 142, 25)];
        textfeild.textAlignment=NSTextAlignmentLeft;
        textfeild.placeholder=@"无";
        textfeild.tag=i+10;
        textfeild.delegate=self;
        textfeild.textColor=[UIColor lightGrayColor];
        if (kScreenWidth==414) {
            textfeild.font=[UIFont systemFontOfSize:17];
        }else
        {
            textfeild.font=[UIFont systemFontOfSize:15];
        }        [cell addSubview:textfeild];
        [cellArray addObject:cell];
    }

}
- (void)viewWillAppear:(BOOL)animated
{
    NSUserDefaults *userDefult=[NSUserDefaults standardUserDefaults];
    if ([[userDefult objectForKey:@"archives"] isEqualToString:@"1"]&&[[userDefult objectForKey:@"invite"] isEqualToString:@"1"]) {
        self.sliderLabel.text=@"100%";
        self.slider.value=1.0;
        self.sliderLabel.frame=CGRectMake((kScreenWidth-50)+4,self.slider.top-37, 43, 21);
        self.sliderBub.frame=CGRectMake((kScreenWidth-50)+4,self.slider.top-38, 43, 32);
    }else if([[userDefult objectForKey:@"archives"] isEqualToString:@"1"]&&[[userDefult objectForKey:@"invite"] isEqualToString:@"0"])
    {
        self.sliderLabel.text=@"80%";
        self.slider.value=0.8;
        self.sliderLabel.frame=CGRectMake((kScreenWidth-50)/5*4+4,self.slider.top-37, 43, 21);
        self.sliderBub.frame=CGRectMake((kScreenWidth-50)/5*4+4,self.slider.top-38, 43, 32);
    }else if ([[userDefult objectForKey:@"archives"] isEqualToString:@"0"]&&[[userDefult objectForKey:@"invite"] isEqualToString:@"1"])
    {
        self.sliderLabel.text=@"20%";
        self.slider.value=0.2;
    }else{
        self.sliderLabel.text=@"20%";
        self.slider.value=0.2;
        self.sliderLabel.frame=CGRectMake((kScreenWidth-50)/5+4,self.slider.top-37, 43, 21);
        self.sliderBub.frame=CGRectMake((kScreenWidth-50)/5+4,self.slider.top-38, 43, 32);
    }
    if (self.slider.value>=0.8) {
        [self.nextButton setBackgroundImage:[UIImage imageNamed:@"finish.png"] forState:UIControlStateNormal];
        self.nextButton.userInteractionEnabled=YES;
    }
    else
    {
        [self.nextButton setBackgroundImage:[UIImage imageNamed:@"obtain_unfinish.png"] forState:UIControlStateNormal];
        self.nextButton.userInteractionEnabled=NO;
    }

}
/**
 *  返回
 */
- (void)viewDidLoad
{
    [super viewDidLoad];
    [self makeNavBar];
    self.navigationItem.title=@"添加档案";
    
    self.cellDic=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"",@"familyDisease",@"",@"geneticDisorders",@"",@"chronicDiseases", nil];
    
    UserModel *userModel = [[UserManager shareInstance]getUserInfo];
    NSString *uid=userModel.uid;
    NSUserDefaults *userdefault=[NSUserDefaults standardUserDefaults];
    
    NSString *gender=[NSString stringWithFormat:@"%@",[userdefault valueForKey:[NSString stringWithFormat:@"%@gender",uid]]];
    if ([gender integerValue]>0) {
        if ([gender integerValue]>1) {
            self.allDic=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"女",@"sex",@"",@"birthday",@"",@"height",@"",@"weight", nil];
        }else
        {
            self.allDic=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"男",@"sex",@"",@"birthday",@"",@"height",@"",@"weight", nil];
        }
    }
    else
    {
        self.allDic=[[NSMutableDictionary alloc]initWithObjectsAndKeys:@"",@"sex",@"",@"birthday",@"",@"height",@"",@"weight", nil];
    }
    self.slider=[[UISlider alloc]init];
    if(kScreenHeight==IPHONE_HEIGHT)
        {
            self.slider.frame=CGRectMake(25, 47, kScreenWidth-50, 2);
        }
    else
        {
            self.slider.frame=CGRectMake(25, 57, kScreenWidth-50, 2);
        }
    self.sliderLabel.frame=CGRectMake(3.5,self.slider.top-37, 43, 21);
    self.sliderBub.frame=CGRectMake(3.5,self.slider.top-38, 43, 32);
    self.tableview.frame=CGRectMake(0, kScreenHeight==IPHONE_HEIGHT? self.slider.bottom+10:self.slider.bottom+30, kScreenWidth, IPhone6Plus?380:340);

    self.tableview.delegate=self;
    self.tableview.dataSource=self;
    self.view.backgroundColor=[ClassMethodsViewController colorWithHexString:BACK_BACKGROUNDCOLOR_16];
    self.tableview.backgroundColor=[UIColor clearColor];

    [self.view addSubview:self.tableview];
    self.slider.backgroundColor = [UIColor clearColor];
    self.slider.value=0.2;
    self.slider.userInteractionEnabled=NO;
    self.slider.minimumValue=0.0;
    self.slider.maximumValue=1.0;
    [self.slider setMinimumTrackImage:[UIImage imageNamed:@"slider_left.png"] forState:UIControlStateNormal];
    [self.slider setMaximumTrackImage:[UIImage imageNamed:@"slider_right.png"] forState:UIControlStateNormal];
    [self.slider setThumbImage:[UIImage imageNamed:@"slider_center.png"] forState:UIControlStateNormal];
    [self.view addSubview:self.slider];
    self.tableview.separatorStyle=UITableViewCellSeparatorStyleNone;
    
    
    self.nextButton.userInteractionEnabled=NO;
    self.nextButton.frame=CGRectMake(15, kScreenHeight-56-64, kScreenWidth-30, 46);
    [self creatUI];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHidden:) name:UIKeyboardWillHideNotification object:nil];

}
- (void)viewWillDisappear:(BOOL)animated
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)keyboardWillHidden:(NSNotification *)not
{
    [self viewScrolltobottom];
}

#pragma  mark tableviewdelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section==0) {
        return 4;
    }
    else return 3;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0) {
        static  NSString *indentifier=@"cell";
        ArchiveViewCell *cell=[tableView dequeueReusableCellWithIdentifier:indentifier];
        if (cell==nil) {
            if (IPhone6Plus) {
                cell=[[[NSBundle mainBundle]loadNibNamed:@"ArchiveViewCell" owner:self options:nil] lastObject];
            }else
            {
                cell=[[[NSBundle mainBundle]loadNibNamed:@"ArchiveViewCell" owner:self options:nil] firstObject];

            }
        }
        cell.itemDetail.tag=indexPath.row+1;
        cell.itemDetail.delegate=self;
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        if (indexPath.row==0) {
            cell.itemDetail.inputView=myPickerView;

            cell.itemBackground.image=[UIImage imageNamed:@"archive_sex"];
            cell.itemName.text=@"性别";
            cell.itemDetail.placeholder=@"请输入您的性别";
            cell.itemDetail.text=[self.allDic valueForKey:@"sex"];
        }else if(indexPath.row==1){
            cell.itemDetail.inputView=self.datePicker;
            cell.itemBackground.image=[UIImage imageNamed:@"archive_ birthday"];
            cell.itemDetail.text=[self.allDic valueForKey:@"birthday"];
            cell.itemDetail.placeholder=@"请输入您的年龄";
            cell.itemName.text=@"年龄";
        }
        else if (indexPath.row==2){
            cell.itemDetail.inputView=myPickerView;

            cell.itemBackground.image=[UIImage imageNamed:@"archive_heigh"];
            cell.itemName.text=@"身高";
            cell.itemDetail.text=[self.allDic valueForKey:@"height"];
            cell.itemDetail.placeholder=@"请输入您的身高";
        }
        else{
            cell.itemDetail.inputView=myPickerView;

            cell.itemBackground.image=[UIImage imageNamed:@"archvie_ weight"];
            cell.itemName.text=@"体重";
            cell.itemDetail.text=[self.allDic valueForKey:@"weight"];
            cell.itemDetail.placeholder=@"请输入您的体重";

        }
        return cell;
    }else
    {
        FamilyHistoryCell *cell=[cellArray objectAtIndex:indexPath.row];
               if (indexPath.row==0) {
            cell.itemName.text=@"家族病史";
        }else if (indexPath.row==1){
            cell.itemName.text=@"传染病史";
        }else{
            cell.itemName.text=@"慢性病史";
        }
        return cell;
    }
}

//动画  上
-(void)viewScrolltoTop:(NSInteger)tag
{
    CGRect frame;
    if (tag==10) {
        frame=CGRectMake(0, -60, kScreenWidth, kScreenHeight);
    }else if(tag==11){
        frame=CGRectMake(0, -80, kScreenWidth, kScreenHeight);
    }else if(tag ==12)
    {
        frame=CGRectMake(0, -95, kScreenWidth, kScreenHeight);
    }
    [UIView animateWithDuration:0.3 animations:^{
        self.view.frame=frame;
    } completion:^(BOOL finished) {
    }];
}
//动画  下
- (void)viewScrolltobottom
{
    [UIView animateWithDuration:0.3 animations:^{
        self.view.frame=CGRectMake(0,64, kScreenWidth, kScreenHeight);
    } completion:^(BOOL finished) {
    }];

}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 10)];
    return view;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section==0) {
        return 0;
    }
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (kScreenWidth==414) {
        return 48;
    }
    return 40;
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    if (textField.tag==1) {
        [self.cellDic setObject:textField.text forKey:@"familyDisease"];
    }else if (textField.tag==2){
        [self.cellDic setObject:textField.text forKey:@"geneticDisorders"];
    }
    else{
        [self.cellDic setObject:textField.text forKey:@"chronicDiseases"];
    }
    [self viewScrolltobottom];
    return YES;
}
#pragma  mark
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [self.xinsView removeFromSuperview];

    if (textField.tag==1) {
        selectedType = selectSex;
        pickerViewDate = sexArray;
        beforeSelectedString = textField.text;
        selectedString = [sexArray objectAtIndex:0];
        [myPickerView reloadAllComponents];
        [myPickerView selectRow:0 inComponent:0 animated:NO];
    }
    else if (textField.tag==2){
//        selectedType = selectAge;
//        pickerViewDate = ageArray;
//        beforeSelectedString = textField.text;
//        selectedString = [ageArray objectAtIndex:29];
//        [myPickerView reloadAllComponents];
//        [myPickerView selectRow:29 inComponent:0 animated:NO];
        UITextField *textField1 = (UITextField *)[self.view viewWithTag:1];
        UITextField *textField3= (UITextField *)[self.view viewWithTag:3];
        UITextField *textField4 = (UITextField *)[self.view viewWithTag:4];

        [textField1 resignFirstResponder];
        [textField3 resignFirstResponder];
        [textField4 resignFirstResponder];


        self.xinsView = [[UIView alloc] initWithFrame:CGRectMake(0, kScreenHeight -64- 200, kScreenWidth, 200)];
        [self.view addSubview:self.xinsView];
        self.datePicker = [[HYDatePicker alloc]initWithDate:nil type:HYDatePickerTypePlain showInView:self.xinsView handler:^(HYDatePicker *picker, BOOL finished) {
            float swiftX=(kScreenWidth-50)/5*15/20;

            if (0 == [picker getDateString].length) {
                textField.text = [self.allDic valueForKey:@"birthday"];
            }else{
                textField.text = [picker getDateString];
                //                [selfd _parsePersonalDataSource:kBaseURL];
                UITextField *textField = (UITextField *)[self.view viewWithTag:2];
                textField.text = [picker getDateString];                
                if ([[self.allDic valueForKey:@"birthday"] isEqualToString:@""]) {
                    self.sliderLabel.frame=CGRectMake(self.sliderLabel.left+swiftX, self.slider.top-37, 43, 21);
                    self.sliderBub.frame=CGRectMake(self.sliderBub.left+swiftX, self.slider.top-38, 43, 32);
                    self.slider.value=self.slider.value+0.15;
                    self.sliderLabel.text=[NSString stringWithFormat:@"%.f%%",self.slider.value*100];
                }
                [self.allDic setValue:[picker getDateString] forKey:@"birthday"];
                [self buttonUserInfo];

                [textField resignFirstResponder];

            }
            [textField resignFirstResponder];
            [self.xinsView removeFromSuperview];
        }];
        [self.datePicker show];
        return NO;
    }
    else if (textField.tag==3){
        selectedType = selectHeight;
        pickerViewDate = heightArray;
        beforeSelectedString = textField.text;
        selectedString = [heightArray objectAtIndex:70];
        [myPickerView reloadAllComponents];
        [myPickerView selectRow:70 inComponent:0 animated:NO];
    }
    else if (textField.tag==4){
        selectedType = selectWeight;
        pickerViewDate = weightArray;
        beforeSelectedString = textField.text;
        selectedString = [weightArray objectAtIndex:30];
        [myPickerView reloadAllComponents];
        [myPickerView selectRow:30 inComponent:0 animated:NO];
    }
    else
    {
        [self viewScrolltoTop:textField.tag];
    }
//    commitBtnImg.enabled= YES;//有改变，让可以提交
    return YES;

}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField.tag==2) {
        [textField resignFirstResponder];
    }
}
#pragma MyPickerViewDelegate
- (NSInteger)numberOfComponentsInMyPickerView:(MyPickerView *)myPickerView{
    return 1;
}

- (NSInteger)myPickerView:(MyPickerView *)myPickerView numberOfRowsInComponent:(NSInteger)component{
    return pickerViewDate.count;
}

- (CGFloat)myPickerView:(MyPickerView *)myPickerView rowHeightForComponent:(NSInteger)component{
    return 25;
}

- (NSString *)myPickerView:(MyPickerView *)myPickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [pickerViewDate objectAtIndex:row];
}

- (void)myPickerView:(MyPickerView *)myPickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    selectedString = [pickerViewDate objectAtIndex:row];
    if (selectedType == selectSex) {
        UITextField *textField = (UITextField *)[self.view viewWithTag:1];
        textField.text = selectedString;
    }
    else if (selectedType == selectAge){
        UITextField *textField = (UITextField *)[self.view viewWithTag:2];
        textField.text = selectedString;
    }
    else if (selectedType == selectHeight){
        UITextField *textField = (UITextField *)[self.view viewWithTag:3];
        textField.text = selectedString;
    }
    else if (selectedType == selectWeight){
        UITextField *textField = (UITextField *)[self.view viewWithTag:4];
        textField.text = selectedString;
    }
}
- (void)myPickerViewCancelSelected:(MyPickerView *)myPickerView
{
    if (selectedType == selectSex) {
        UITextField *textField = (UITextField *)[self.view viewWithTag:1];
        textField.text = beforeSelectedString;
        [textField resignFirstResponder];
    }
    else if (selectedType == selectAge){
//        UITextField *textField = (UITextField *)[self.view viewWithTag:2];
//        textField.text = beforeSelectedString;
//        [textField resignFirstResponder];
    }
    else if (selectedType == selectHeight){
        UITextField *textField = (UITextField *)[self.view viewWithTag:3];
        textField.text = beforeSelectedString;
        [textField resignFirstResponder];
    }
    else if (selectedType == selectWeight){
        UITextField *textField = (UITextField *)[self.view viewWithTag:4];
        textField.text = beforeSelectedString;
        [textField resignFirstResponder];
    }
}
- (void)myPickerViewSureSelected:(MyPickerView *)myPickerView
{
    float swiftX=(kScreenWidth-50)/5*15/20;

    if (selectedType == selectSex) {
        UITextField *textField = (UITextField *)[self.view viewWithTag:1];
        textField.text = selectedString;
        
        if ([[self.allDic valueForKey:@"sex"] isEqualToString:@""]) {
            self.sliderLabel.frame=CGRectMake(self.sliderLabel.left+swiftX, self.slider.top-37, 43, 21);
            self.sliderBub.frame=CGRectMake(self.sliderBub.left+swiftX, self.slider.top-38, 43, 32);
            self.slider.value=self.slider.value+0.15;
            self.sliderLabel.text=[NSString stringWithFormat:@"%.f%%",self.slider.value*100];
        }
        [self.allDic setValue:selectedString forKey:@"sex"];
        [textField resignFirstResponder];
    }
    else if (selectedType == selectAge){
//        UITextField *textField = (UITextField *)[self.view viewWithTag:2];
//        textField.text = selectedString;
//        [self.allDic setValue:selectedString forKey:@"birthday"];
//
//        if (![[self.allDic valueForKey:@"birthday"] isEqualToString:@""]) {
//            self.sliderLabel.frame=CGRectMake(self.sliderLabel.left+40, self.slider.top-37, 43, 21);
//            self.sliderBub.frame=CGRectMake(self.sliderBub.left+40, self.slider.top-38, 43, 32);
//            self.slider.value=self.slider.value+0.15;
//            self.sliderLabel.text=[NSString stringWithFormat:@"%.f%%",self.slider.value*100];
//        }
//        [self.allDic setValue:selectedString forKey:@"birthday"];
//        [textField resignFirstResponder];
    }
    else if (selectedType == selectHeight){
        UITextField *textField = (UITextField *)[self.view viewWithTag:3];
        textField.text = selectedString;

        if ([[self.allDic valueForKey:@"height"] isEqualToString:@""]) {
            self.sliderLabel.frame=CGRectMake(self.sliderLabel.left+swiftX, self.slider.top-37, 43, 21);
            self.sliderBub.frame=CGRectMake(self.sliderBub.left+swiftX, self.slider.top-38, 43, 32);
            self.slider.value=self.slider.value+0.15;
            self.sliderLabel.text=[NSString stringWithFormat:@"%.f%%",self.slider.value*100];
        }
        [self.allDic setValue:selectedString forKey:@"height"];
        [textField resignFirstResponder];
    }
    else if (selectedType == selectWeight){
        UITextField *textField = (UITextField *)[self.view viewWithTag:4];
        textField.text = selectedString;
        
        if ([[self.allDic valueForKey:@"weight"] isEqualToString:@""]) {
            self.sliderLabel.frame=CGRectMake(self.sliderLabel.left+swiftX, self.slider.top-37, 43, 21);
            self.sliderBub.frame=CGRectMake(self.sliderBub.left+swiftX, self.slider.top-38, 43, 32);
            self.slider.value=self.slider.value+0.15;
            self.sliderLabel.text=[NSString stringWithFormat:@"%.f%%",self.slider.value*100];
        }
        [self.allDic setValue:selectedString forKey:@"weight"];
        [textField resignFirstResponder];
    }
    [self buttonUserInfo];
}
- (IBAction)doNext:(UIButton *)sender
{
    [self showActionView];
    NSString * postURL = [NSString stringWithFormat:@"%@user/archivesadd/",kBaseURL];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *gender;
    if ([[self.allDic valueForKey:@"sex"] isEqualToString:@"男"]) {
        gender=@"1";
    }
    else{
        gender=@"2";
    }
    NSString *isfamilial;
    NSString *familial_disease;
    //是否有家族病
    if ([[self.cellDic valueForKey:@"familyDisease"] isEqualToString:@""]) {
        isfamilial=@"0";
        familial_disease=@"";
    }else{
        isfamilial=@"1";
        familial_disease=[self.cellDic valueForKey:@"familyDisease"];
    }
    NSString *isinfectious;
    NSString *infectious_disease;
    //是否有家族病
    if ([[self.cellDic valueForKey:@"geneticDisorders"] isEqualToString:@""]) {
        isinfectious=@"0";
        infectious_disease=@"";
    }else{
        isinfectious=@"1";
        infectious_disease=[self.cellDic valueForKey:@"geneticDisorders"] ;
    }
    NSString *ischronic;
    NSString *chronic_disease;
    
    //是否有家族病
    if ([[self.cellDic valueForKey:@"chronicDiseases"] isEqualToString:@""]) {
        ischronic=@"0";
        chronic_disease=@"";
    }else{
        ischronic=@"1";
        chronic_disease=[self.cellDic valueForKey:@"chronicDiseases"];
    }
    NSRange range=[[self.allDic valueForKey:@"weight"] rangeOfString:@"k"];
    NSString *weight=[[self.allDic valueForKey:@"weight"] substringToIndex:range.location];
    NSDictionary *parameters = @{
                                 @"platform": kPlatform,
                                 @"version":kVersion,
                                 @"gender":gender,
                                 @"birthday":[self.allDic valueForKey:@"birthday"],
                                 @"height":[[self.allDic valueForKey:@"height"] substringWithRange:NSMakeRange(0,3)],
                                 @"weight":weight,
                                 @"isfamilial":isfamilial,
                                 @"familial_disease":familial_disease,
                                 @"isinfectious":isinfectious,
                                 @"infectious_disease":infectious_disease,
                                 @"ischronic":ischronic,
                                 @"chronic_disease":chronic_disease,
                                 };
    [manager POST:postURL parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *status=[NSString stringWithFormat:@"%@",[responseObject valueForKey:@"status"]];
        [self hiddenActionView];
        if ([status isEqualToString:@"1"]) {
            NSUserDefaults *userDefult=[NSUserDefaults standardUserDefaults];
            [userDefult setObject:@"1" forKey:@"archives"];
            UserModel *userModel = [[UserManager shareInstance]getUserInfo];
            [userDefult setObject:gender forKey:[NSString stringWithFormat:@"%@gender",userModel.uid]];
            NSString *currentAge=[self calculateAge:[self.allDic valueForKey:@"birthday"]];
            [userDefult setObject:currentAge forKey:[NSString stringWithFormat:@"%@age",userModel.uid]];
            InviteFriendsViewController *friends=[[InviteFriendsViewController alloc]initWithNibName:@"InviteFriendsViewController" bundle:[NSBundle mainBundle]];
            [self.navigationController pushViewController:friends animated:YES];
            
        }else
        {
            [self showCustomHudSingleLine:[responseObject valueForKey:@"msg"]];
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
    }];

}
- (void)buttonUserInfo
{
    if (self.slider.value>=0.7) {
        [self.nextButton setBackgroundImage:[UIImage imageNamed:@"finish.png"] forState:UIControlStateNormal];
        self.nextButton.userInteractionEnabled=YES;
    }
    else
    {
        [self.nextButton setBackgroundImage:[UIImage imageNamed:@"obtain_unfinish.png"] forState:UIControlStateNormal];
        self.nextButton.userInteractionEnabled=NO;
    }
}
/**
 *  出生年月，计算实际年龄
 */
- (NSString *)calculateAge:(NSString *)age
{
    NSArray *array=[age componentsSeparatedByString:@"-"];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    NSString *strDate = [dateFormatter stringFromDate:[NSDate date]];
    NSArray *currentArray=[strDate componentsSeparatedByString:@"-"];
    int agee=0;
    int year=[[currentArray objectAtIndex:0] intValue]-[[array objectAtIndex:0] intValue];
    int month=[[currentArray objectAtIndex:1] intValue]-[[array objectAtIndex:1] intValue];
    int day=[[currentArray objectAtIndex:2] intValue]-[[array objectAtIndex:2] intValue];
    if (year>0) {
        agee=year-1;
    }
    if (month>0) {
        agee=agee+1;
    }else if (month==0)
    {
        if (day>=0) {
            agee=agee+1;
        }
    }
    return [NSString stringWithFormat:@"%d",agee];
}
@end
